<?php

namespace App\Admin\Actions\Batch;

use App\Library\Logic\OperateLogLogic;
use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Actions\Response;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Collection;

class BatchShopProductDown extends BatchAction
{
    public $name = '下架';

    /**
     * handle
     * @param Collection $collection
     * @return Response
     */
    public function handle(Collection $collection): Response
    {
        foreach ($collection as $model) {
            if ($model->product_status === 1) continue;
            $model->product_status = 1;
            $model->product_offline_time = now()->timestamp;
            $model->operated_at = now();
            $model->save();
        }
        OperateLogLogic::add(Admin::user()->user_id, 'admin.productBatch.down');
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * dialog
     * @return void
     */
    public function dialog(): void
    {
        $this->confirm('确定下架?');
    }
}
